import service.User;
import service.UserCglibProxy;
import sun.misc.ProxyGenerator;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author zhu_shuai
 * @since 8/4
 */
public class Demo02Cglib {
    public static void main(String[] args) throws IOException {

        User user = new User();
        UserCglibProxy userCglibProxy = new UserCglibProxy(user);
        User proxyUser = (User) userCglibProxy.getProxyInstance();
        proxyUser.setUserName("王五");
        proxyUser.getUserName();

        // 将代理类字节码保存到本地，反编译后查看继承了Proxy类
        byte[] proxyClass = ProxyGenerator.generateProxyClass(proxyUser.getClass()
                .getSimpleName(), proxyUser.getClass().getInterfaces());
        FileOutputStream fileOutputStream = new FileOutputStream(new File("e://proxy1.class"));
        fileOutputStream.write(proxyClass);
        fileOutputStream.flush();
        fileOutputStream.close();

    }
}
